import Vue from "vue";
import lodash from "lodash";
import store from '@/store';
import {api} from '../../service2/scenario'
const modelState=store.state.admin.model;

const util= {
    Vue,
    ...store.state.tools,
    api:api,
    modelState,
    getTypeListByModel: (model) => {
        if(!model){
            return [];
        }
        let typeRow = modelState.modelTree.filter(item => item.value.startsWith(model));
        if (!!typeRow) {
            return typeRow;
        } else {
            return [];
        }
    },
    getClassListByType: (type) => {
        let typeRow = modelState.modelTree.find(item => item.value === type);
        if (!!typeRow) {
            return typeRow.children;
        } else {
            return [];
        }
    },
    // 根据type和className获取values字段中所有key
    getKeyByTypeClass: (type,className) => {
        if(!type || !className){
            return [];
        }
        let valueObj = modelState.map.get(`${type},${className}`);
        let featureList = valueObj.featureList;
        let valueList = valueObj.valueList;
        let keyArr = [...featureList,...valueList];
        return keyArr;
    },
    getModel(type,className){
        return modelState.all.filter(item=>item.type===type&&item.className===className)
    },
    getCascader: (data, first, second ) => {
        let tree = [];
        data.map(item => {
            let index = tree.findIndex((m) => m.value === lodash.get(item, first));
            if (index === -1) {
                let obj = {
                    label: lodash.get(item, first),
                    value: lodash.get(item, first),
                    children: [
                        {
                            label:lodash.get(item, second),
                            labelRender: `${lodash.get(item, second)} \n ver.:${lodash.get(item, 'version')},pub.:${lodash.get(item, 'published')}`,
                            value: lodash.get(item, second),
                            id:lodash.get(item, 'id'),
                        },
                    ],
                };
                tree.push(obj);
            } else {
                tree[index].children.push({
                    label:lodash.get(item, second),
                    labelRender: `${lodash.get(item, second)} \n ver.:${lodash.get(item, 'version')},pub.:${lodash.get(item, 'published')}`,
                    value: lodash.get(item, second),
                    id:lodash.get(item, 'id'),
                });
            }
        });
        return tree;
    },
    getTree: (data, first, second) => {
        let tree = [];
        data.map(item => {
            let index = tree.findIndex((m) => m.value === lodash.get(item, first));
            if (index === -1) {
                let obj = {
                    title: lodash.get(item, first),
                    value: lodash.get(item, first),
                    id:lodash.get(item, 'id'),
                    expand: true,
                    children: [
                        {
                            title:lodash.get(item, second),
                            titleRender: `${lodash.get(item, second)} \n ver.:${lodash.get(item, 'version')},pub.:${lodash.get(item, 'published')}`,
                            value: [lodash.get(item, first), lodash.get(item, second)],
                            id:lodash.get(item, 'id'),
                            contextmenu: true,
                        },
                    ],
                };
                tree.push(obj);
            } else {
                tree[index].children.push({
                    title:lodash.get(item, second),
                    titleRender: `${lodash.get(item, second)} \n ver.:${lodash.get(item, 'version')},pub.:${lodash.get(item, 'published')}`,
                    value: [lodash.get(item, first), lodash.get(item, second)],
                    id:lodash.get(item, 'id'),
                    contextmenu: true,
                });
            }
        });
        return tree;
    },
    getFilter(filterMap, sign = 'like'){
        let arr = [];
        if(!!filterMap){
            Object.keys(filterMap).map(key=>{
                let curItem = filterMap[key]
                let type = typeof curItem;
                let strWrap = (type ==='number' || type ==='boolean') ? "" : "'"

                if (curItem !== '' && curItem !== undefined && curItem !== null) {
                    if (sign === 'like' && strWrap && key!='className'&& key!='type'&& key!='id') {
                        arr.push(`${key} like ${strWrap}%${curItem}%${strWrap}`)
                    } else {
                        arr.push(`${key} ${"=="} ${strWrap}${curItem}${strWrap}`)
                    }
                }
            })
        };
        return arr.join(' && ');
    },
    /* getFilter(filterMap){
        let str='';
        if(!!filterMap){
            Object.keys(filterMap).map(key=>{
                let type=typeof filterMap[key];
                if(str==''){
                    str=type==='number'||type==='boolean'?`${key}==${filterMap[key]}`:`${key}=='${filterMap[key]}'`
                }else{
                    str=type==='number'||type==='boolean'?str+`&&${key}==${filterMap[key]}`:str+`&&${key}=='${filterMap[key]}'`
                }
            })
        };
        return str;
    } */
}
window.util=util;
export default util;